<template>
    <div v-show="rateType=='0' || rateType=='1'">
        <Row>
            <Col>
                <Alert>
                    <h2>标准利率值</h2>
                </Alert>
                <Row class="operation">
                    <Button @click="add" icon="md-add" type="primary" v-show="parentParam.status!=3">新增</Button>
                </Row>
                <div v-if="rateType=='0'">
                    <Row>
                        <Table :columns="columns" :data="planRatejson" :loading="loading"
                               border ref="table">
                            <template slot="action" slot-scope="{ row, index }">
                                <Button @click="edit(row,index)" size="small" style="margin-right:5px" type="primary" v-show="parentParam.status!=3">编辑</Button>
                                <Button @click="remove(row,index)" size="small" type="error" v-show="parentParam.status!=3">删除</Button>
                            </template>
                            <template slot="custRate" slot-scope="{ row, index }">
                                <span>{{row.custRate+'%'}}</span>
                            </template>
                            <template slot="clearRate" slot-scope="{row, index }">
                                <span>{{row.clearRate+'%'}}</span>
                            </template>
                        </Table>
                    </Row>
                </div>
                <div v-if="rateType=='1'">
                    <Row>
                        <Table :columns="columnsFloat" :data="planRatejson" :loading="loading"
                               border ref="table">
                            <template slot="action" slot-scope="{ row, index }">
                                <Button @click="edit(row,index)" size="small" style="margin-right:5px"
                                        type="primary" v-show="parentParam.status!=3">编辑
                                </Button>
                                <Button @click="remove(row,index)" size="small" type="error"
                                        v-show="parentParam.status!=3">删除
                                </Button>
                            </template>
                            <template slot="clearRate" slot-scope="{row, index }">
                                <span>{{row.clearRate+'%'}}</span>
                            </template>
                        </Table>
                    </Row>
                </div>
            </Col>
            <br>
        </Row>
        <Modal v-model="modalVisible" :title="modalTitle" :width="500" :closable="false"
               :mask-closable="false">
            <Form ref="productPlanRate" :model="productPlanRate" :label-width="180" :rules="productPlanRateValidate">
                <FormItem label="贷款期数起" prop="loanPeriodStart">
                    <InputNumber v-model="productPlanRate.loanPeriodStart"
                                 :step="1" :min="1"
                                 style="margin-right:10px;width:150px"/>
                </FormItem>
                <FormItem label="贷款期数止" prop="loanPeriodEnd">
                    <InputNumber v-model="productPlanRate.loanPeriodEnd"
                                 :step="1" :min="1"
                                 style="margin-right:10px;width:150px"/>
                </FormItem>
                <FormItem label="客户利率%" prop="custRate" v-if="rateType==0">
                    <InputNumber :min='0'
                                 v-model="productPlanRate.custRate"
                                 autocomplete="off"
                                 :formatter="value => `${value}`"
                                 style="margin-right:10px;width:150px"/>
                </FormItem>
                <FormItem label="加点值" prop="addPointValue" v-if="rateType==1">
                    <InputNumber :step="100" :min="0" v-model="productPlanRate.addPointValue" autocomplete="off"
                                 style="margin-right:10px;width:150px"/>
                </FormItem>
                <FormItem label="结算利率%" prop="clearRate" v-if="rateType==0">
                    <InputNumber :min='0'
                                 v-model="productPlanRate.clearRate"
                                 autocomplete="off"
                                 :formatter="value => `${value}`"
                                 style="margin-right:10px;width:150px"/>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" @click="cancel">取消</Button>
                <Button type="primary" @click="okClick">确定</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
    import * as utils from '_p/afs-apply/assets/js/utils.js'
    import {deepClone} from "@/libs/utils/ObjectClone";

    import {
        getLprNew,
    } from "@/projects/afs-core-business/api/afs-product/product/lpr";

    export default {
        name: "product-plan-rate",
        props: {
            parentParam: {
                Object,
            },
            planRatejson: {
                Array
            },
            rateType: {
                type: String,
                require: true
            }
        },
        data() {
            const validateDicmal = (rule, value, callback) => {
                if (value) {
                    if (!utils.isTwoDicmal(value)) {
                        callback(new Error("格式数字且最多为两位小数"))
                    } else {
                        callback();
                    }
                }
            };
            const validateInteger = (rule, value, callback) => {
                if (!utils.isPositiveInteger(value)) {
                    callback(new Error("格式为正整数"))
                } else {
                    callback();
                }
            };
            return {
                loading: false,
                data: [],
                data1: [],
                data2: [],
                modalTitle: '',
                modalVisible: false,
                submitLoading: false,
                modalType: null,
                ishow: true,
                productPlanRates: {},
                productPlanRate: {
                    id: null,
                    loanPeriodStart: null,
                    loanPeriodEnd: null,
                    addPointValue: null,
                    custRate: 0,
                    clearRate: 0,
                    lpr:null
                },
                productPlanRateValidate: {
                    loanPeriodStart: [
                        {required: true, message: "贷款期数起不能为空", type: 'number', trigger: "change"},

                    ],
                    loanPeriodEnd: [
                        {required: true, message: "贷款期数止不能为空", type: 'number', trigger: "change"},

                    ],
                    custRate: [
                        {required: true, message: "客户利率不能为空", type: 'number', trigger: "change"},

                    ],
                    clearRate: [
                        {required: true, message: "结算利率不能为空", type: 'number', trigger: "change"},

                    ],
                    addPointValue: [
                        {required: true, message: "加点值不能为空", type: 'number', trigger: "change"},
                    ]
                },
                columns: [
                    {
                        title: "贷款期数起",
                        key: "loanPeriodStart",
                        align: "center"
                    },
                    {
                        title: "贷款期数止",
                        key: "loanPeriodEnd",
                        align: "center"
                    },
                    {
                        title: "客户利率",
                        key: "custRate",
                        slot: "custRate",
                        align: "center"
                    },
                    {
                        title: "结算利率",
                        key: "clearRate",
                        slot: "clearRate",
                        align: "center"
                    },
                    {
                        title: "操作",
                        key: "action",
                        align: "center",
                        slot: "action"
                    }
                ],
                columnsFloat: [
                    {
                        title: "贷款期数起",
                        key: "loanPeriodStart",
                        align: "center"
                    },
                    {
                        title: "贷款期数止",
                        key: "loanPeriodEnd",
                        align: "center"
                    },
                    {
                        title: "当前LPR",
                        key: "lpr",
                        align: "center"
                    },
                    {
                        title: "加点值",
                        key: "addPointValue",
                        align: "center"
                    },
                    {
                        title: "结算利率",
                        key: "clearRate",
                        slot: "clearRate",
                        align: "center"
                    },
                    {
                        title: "操作",
                        key: "action",
                        align: "center",
                        slot: "action"
                    }
                ]
            }
        },
        watch: {
            'rateType': {
                handler: function (val, oldVal) {
                    if ('0' == val && '1' == oldVal) {
                        this.data2 = this.planRatejson;
                        this.$emit('rateChange', this.data1)
                    } else if ('1' == val && '0' == oldVal) {
                        this.data1 = this.planRatejson;
                        this.$emit('rateChange', this.data2)
                    }
                },
            }
        },
        created() {
        },
        methods: {
            cancel() {
                this.modalTitle = "";
                if ("1" == this.modalType) {
                    this.planRatejson.push(this.productPlanRates);
                }
                this.modalVisible = false;
            },
            okClick() {
                let self = this;
                self.$refs.productPlanRate.validate(validate => {
                    if (validate) {
                        if (this.productPlanRate.loanPeriodStart > this.productPlanRate.loanPeriodEnd) {
                            this.$Message.error("贷款期数起不能大于贷款期数止");
                            return;
                        }
                        if(this.rateType=='1'){
                            getLprNew(this.productPlanRate).then(res => {
                                if (res.code === "0000") {
                                    let lprRate = res.data+this.productPlanRate.addPointValue/100;
                                    this.productPlanRate.clearRate = lprRate.toFixed(2);
                                    this.productPlanRate.lpr = res.data;
                                    this.planRatejson.push(this.productPlanRate);
                                }
                            });
                        }else{
                            this.planRatejson.push(this.productPlanRate);
                        }
                        var colId="loanPeriodStart"
                        //对json进行升序排序函数
                        var asc = function(x,y)
                        {
                            return (x[colId] > y[colId]) ? 1 : -1
                        }
                        this.planRatejson.sort(asc);
                        self.modalVisible = false;
                    } else {
                        self.$Message.error("数据错误");
                    }
                })
            },
            add() {
                this.modalTitle = "新增";
                this.modalType = 0;
                this.productPlanRate = {
                    id: null,
                    loanPeriodStart: null,
                    loanPeriodEnd: null,
                    addPointValue: null,
                    custRate: null,
                    clearRate: null,
                };
                this.modalVisible = true;

            },
            remove(row, index) {
                let self = this;
                self.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除?",
                    onOk: () => {
                        self.planRatejson.splice(index, 1);
                    }
                });
            },
            edit(row, index) {
                this.modalTitle = '编辑';
                this.modalType = 1;
                this.productPlanRate = (deepClone(row));
                this.productPlanRates = (deepClone(row));
                this.planRatejson.splice(index, 1);
                this.modalVisible = true;
            },
        },
    }

</script>

